
package com.hyst.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hyst.tableInfo.ColumnInfo;
import com.hyst.tableInfo.TableModel;
import com.hyst.tool.StringUtil;
import com.hyst.util.MyConfig;

/**
 * @Description:
 * @Date：2017年6月20日 @author： DongY
 * @version ：1.0 1.[2017年6月20日]CreatBy：dy
 */
@Component("doWriteModel")
public class DoWriteModel implements CreatFile{
	@Autowired
	private MyConfig myConfig;

	public void write(TableModel tableModel) throws IOException {

		File file = new File(myConfig.getFilePth() + "/model/");
//		if (myConfig.isCleaPath()&&file.exists()) {
//			File[] files = file.listFiles();
//			for (int i = 0; i < files.length; i++) {
//				File tmp = files[i];
//				String name=tmp.toString();
//				if (name.endsWith(".java")) {
//					name=name.substring(0, name.length()-5);
//					if (!myConfig.getCreatTableNames().contains(name.th())) {
//						tmp.delete();
//					}
//				}
//			}
//		}else if (file.exists()) {
//			File[] files = file.listFiles();
//			for (int i = 0; i < files.length; i++) {
//				File tmp = files[i];
//				if (tmp.toString().endsWith(tableModel.getModelName()+".java")) {
//					tmp.delete();
//				}
//			}
//		}
		if (!file.exists()) {
			file.mkdirs();
		}

		Writer writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file.getPath() + "\\" + tableModel.getModelName() + ".java"), "Utf-8"));

		String string = getPackageAndImports(tableModel);
		string += getClassStart(tableModel);
		string += getFieds(tableModel);
		string += getMethodsStr(tableModel);
		string += "\r\n}";
		writer.write(string);

		writer.flush();

		writer.close();
	}

	public String getClassStart(TableModel tableModel) {
		String string = "@Table(name = \"" + tableModel.getTableName() + "\")\r\n";
		string += "@XmlRootElement(name = \"" + StringUtil.headLowerCase(tableModel.getModelName()) + "\")\r\n";
		string += "public class " + tableModel.getModelName() + "{\r\n";
		List<String> ann = myConfig.getModelAnn();
		// for(String str:ann){
		//
		// }
		return string;
	}

	public String getMethodsStr(TableModel tableModel) {
		String str = "";
		String to = "";
		List<ColumnInfo> columnInfos = tableModel.getFiedls();
		for (ColumnInfo col : columnInfos) {
			String get = "";
			if (!StringUtil.isEmpty(col.getComment())) {
				str += "/**\r\n*设置" + col.getComment() + "\r\n*@param " + col.getFiledName() + "\r\n*/\r\n";
				get += "/**\r\n*获取" + col.getComment() + "\r\n*@return " + col.getFiledName() + "\r\n*/\r\n";
			}
			str += "	public void set" + col.getFiledUpperHeadName() + "(" + col.getJavaType() + " "
					+ col.getFiledName() + "){\r\n" + "		this." + col.getFiledName() + "=" + col.getFiledName()
					+ ";\r\n	}\r\n";
			str += get;
			str += "	public " + col.getJavaType() + " get" + col.getFiledUpperHeadName() + "(){\r\n" + "		return "
					+ col.getFiledName() + ";\r\n	}\r\n";

		}

		return str;
	}

	/**
	 * @Description: 根据表信息生成描述Java实体的字符串
	 * @param tableModel
	 * @return String 返回类型
	 */
	public String getPackageAndImports(TableModel tableModel) {
		String packageName = "package " + myConfig.getPackageName() + ";\r\n";
		Set<String> imports = new HashSet<String>(myConfig.getModelImport());
		if (tableModel.getImports() != null) {

			imports.addAll(tableModel.getImports());
		}
		// Iterator<String> it=imports.iterator();
		// if(it.hasNext()){
		// packageName+="import "+it.next()+";\r\n";
		// }
		for (String imp : imports) {
			packageName += "import " + imp + ";\r\n";
		}
		return packageName;
	}

	/**
	 * @Description: 获取字段
	 * @param tableModel
	 * @return String 返回类型
	 */
	public String getFieds(TableModel tableModel) {
		String fieds = "";
		List<ColumnInfo> columnInfos = tableModel.getFiedls();
		for (int i = 0; i < columnInfos.size(); i++) {
			ColumnInfo columnInfo = columnInfos.get(i);
			String remark = columnInfo.getComment();
			if (!StringUtil.isEmpty(remark)) {
				fieds += "/**\r\n" + remark + "\r\n*/\r\n";
			}
			// if(!StringUtil.isEmpty(myConfig.getChangedFiled())){
			// fieds+=myConfig+"\r\n";
			// }
			// {目前写死之后改为可配置
			fieds += "@FormParam(value=\"" + columnInfo.getFiledName() + "\")\r\n";
			if (columnInfo.isPre()) {
				fieds += "@Id\r\n@GeneratedValue(strategy = GenerationType.IDENTITY)\r\n";
			} else if (!columnInfo.getColumnName().equals(columnInfo.getFiledName())) {
				fieds += "@Column(name = \"" + columnInfo.getColumnName() + "\")\r\n";
			}

			// }
			fieds += "private " + columnInfo.getJavaType() + " " + columnInfo.getFiledName() + ";\r\n";
		}
		return fieds;
	}

}
